<?php

namespace common\models\datas;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\helpers\Util;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%message_log}}".
 *
 * @property integer $id
 * @property integer $site_id
 * @property integer $sender_id
 * @property string $receiver_arr
 * @property string $content
 * @property integer $created_at
 * @property string $send_way
 * @property integer $status
 * @property string $fail_arr_sys
 * @property string $fail_arr_mail
 * @property string $fail_arr_wechat
 * @property string $fail_arr_sms
 * @property integer $admin_id
 * @property integer $type_id
 * @property integer $is_all
 */
class MessageLog extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%message_log}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['site_id', 'sender_id', 'created_at','admin_id', 'type_id', 'is_all'], 'integer'],
            [['receiver_arr', 'fail_arr_sys', 'fail_arr_mail', 'fail_arr_wechat', 'fail_arr_sms'], 'string'],
            [['send_way'], 'required'],
            [['content_sys', 'content_wechat', 'content_mail', 'content_sms'], 'string','max' => 255],
            [['status','send_group'],'string']
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['type_id'] = function () {
            switch ($this->type_id) {
                case 9:
                    return \Yii::t('frontend', '活动消息');
                case 8:
                    return \Yii::t('frontend', '公告消息');
                case 7:
                    return \Yii::t('frontend', '课程消息');
                case 6:
                    return \Yii::t('frontend', '积分消息');
                case 5:
                    return \Yii::t('frontend', '个人资料');
                case 4:
                    return \Yii::t('frontend', '账户信息');
                case 3:
                    return \Yii::t('frontend', '点评');
                case 2:
                    return \Yii::t('frontend', '成长日志');
                case 1:
                    return \Yii::t('frontend', '家委会消息');
                case 10:
                    return \Yii::t('frontend','处理结果');
            }
        };
        $fields['send_way'] = function () {
            $send = unserialize($this->send_way);
            $arr = [];
            foreach ($send as $v) {
                switch ($v) {
                    case 'jpush':
                        $arr[$v] = \Yii::t('frontend', '极光推送');
                        continue;
                    case 'sms':
                        $arr[$v] = \Yii::t('frontend', '短信');
                        continue;
                    case 'wechat':
                        $arr[$v] = \Yii::t('frontend', '微信');
                        continue;
                    case 'email':
                        $arr[$v] = \Yii::t('frontend', '邮件');
                        continue;
                    case 'sys':
                        $arr[$v] = \Yii::t('frontend', '系统');
                        continue;
                }
            }
            return implode('|',$arr);

        };
        $fields['is_all'] = function () {
            switch ($this->is_all) {
                case 0:
                    return \Yii::t('frontend', '否');
                case 1:
                    return \Yii::t('frontend', '是');
            }
        };
        $fields['receiver_arr'] = function () {
            $receiver_arr = unserialize($this->receiver_arr);
            if(!is_array($receiver_arr))return null;
            $text='';
            foreach ($receiver_arr as $k=>$v){
                empty($text)?$text.=$v:$text.="\r\n".'|'.$v;
            }
            return $text;
        };
        $fields['fail_arr_sms'] = function () {
            $fail_arr_sms = unserialize($this->fail_arr_sms);
            if(!is_array($fail_arr_sms))return null;
            $text='';
            foreach ($fail_arr_sms as $k=>$v){
                empty($text)?$text.=$v:$text.="\r\n".'|'.$v;
            }
            return $text;
        };
        $fields['status'] = function () {
            $status = unserialize($this->status);
            return Util::array2text($status);
        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'site_id' => Yii::t('common', '广场编号'),
            'sender_id' => Yii::t('common', '发送者'),
            'receiver_arr' => Yii::t('common', '接受者编号组'),
            'content_sys' => Yii::t('common', '系统内容'),
            'content_wechat' => Yii::t('common', '微信内容'),
            'content_mail' => Yii::t('common', '邮件内容'),
            'content_sms' => Yii::t('common', '短信内容'),
            'created_at' => Yii::t('common', '发送时间'),
            'send_way' => Yii::t('common', '发送方式'),
            'status' => Yii::t('common', '状态'),
            'fail_arr_sys' => Yii::t('common', '系统失败数组'),
            'fail_arr_mail' => Yii::t('common', '邮件失败数组'),
            'fail_arr_wechat' => Yii::t('common', '微信失败数组'),
            'fail_arr_sms' => Yii::t('common', '短信失败数组'),
            'admin_id' => Yii::t('common', '管理员'),
            'type_id' => Yii::t('common', '消息类型'),
            'is_all' => Yii::t('common', '是否全部发送'),
            'send_group' => Yii::t('common', '发送对象'),

        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return $this->admin->username;
            },
//            'frontend_operate' => function () {
//                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR,$this->attributes))->create();
//
//            },

        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }

}
